package miku.firstgame.world
{
	import com.saia.starlingPunk.SP;
	import com.saia.starlingPunk.SPWorld;

	import feathers.controls.Button;
	import feathers.layout.HorizontalLayout;
	import feathers.layout.TiledRowsLayout;

	import miku.firstgame.model.Me;
	import miku.firstgame.model.Room;
	import miku.firstgame.model.User;
	import miku.firstgame.model.gold.UserOrderVo;
	import miku.firstgame.model.proxy.ProxyHelper;
	import miku.firstgame.ui.card.CardGroupUI;
	import miku.firstgame.ui.card.CardMap;
	import miku.firstgame.ui.card.CardUI;
	import miku.firstgame.ui.card.UserSeat;
	import miku.firstgame.ui.component.MessagePanel;
	import miku.firstgame.ui.control.ListEx;
	import miku.firstgame.ui.render.UserPrepareItemRender;
	import miku.firstgame.view.GameRoomWorldMediator;

	import org.puremvc.as3.patterns.facade.Facade;

	import starling.display.Quad;

	public class GameRoomWorld extends SPWorld
	{
		public var room:Room;

		public var startGameButton:Button;
		public var exitRoomButton:Button;

		public var messagePanel:MessagePanel;
		public var userList:ListEx;

		//user for game
		/**
		 * 卡牌地图
		 * */
		public var cardMap:CardMap;
		/**
		 * 用户座位
		 * */
		public var seats:Vector.<UserSeat>;
		/**
		 * 自己的卡牌组
		 * */
		public var cardGroup:CardGroupUI;

		public var currentClock:Quad;

		public function GameRoomWorld(room:Room)
		{
			super();
			this.room = room;
		}

		override public function begin():void
		{
			super.begin();
			initUI();
			Facade.getInstance().registerMediator(new GameRoomWorldMediator(this));
		}

		override public function end():void
		{
			super.end();
			removeAll();
			removeChildren(0, -1, true);
			Facade.getInstance().removeMediator(GameRoomWorldMediator.NAME);
		}

		public function gameStart():void
		{
			this.removeChild(startGameButton);
			this.removeChild(userList);
			initGameUI();
		}

		public function gameStop():void
		{
			this.addChild(startGameButton);
			this.addChild(userList);
			disposeGame();

		}

		public function checkStartButton():void
		{
			if (Me.Instace.getContactUsers().length > 1)
			{
				//startGameButton.isEnabled = true;

				if (Me.Instace.id == room.roomMaster.id)
				{
					startGameButton.label = "开始游戏";
					if (Me.Instace.isAllContactUserReady)
					{
						startGameButton.isEnabled = true;
					}
					else
					{
						startGameButton.isEnabled = false;
					}
				}
				else
				{
					startGameButton.isEnabled = true;
					if (Me.Instace.isReady)
					{
						startGameButton.label = "取消";
					}
					else
					{
						startGameButton.label = "准备";

					}
				}

				/*
				if(Me.Instace.isReady)
				{

				}
				*/
			}
			else
			{
				startGameButton.label = "开始";
				startGameButton.isEnabled = false;
			}
		}

		/**
		 * 使用starlingpunk的体系，而不使用
		 * */
		private function initGameUI():void
		{
			cardMap = new CardMap();
			cardMap.x = 280; //UserSeat.WIDTH + 10;
			cardMap.y = UserSeat.HEIGHT + 40;
			add(cardMap);
			cardMap.initData();

			initSeats();

			cardGroup = new CardGroupUI(Me.Instace.cards);
			cardGroup.y = SP.height - CardUI.HEIGHT - 60;
			cardGroup.x = 360;
			add(cardGroup);

		}

		public function getSeatById(userId:int):UserSeat
		{
			if (seats)
			{
				for each (var seat:UserSeat in seats)
				{
					if (seat.haveUser && seat.user.id == userId)
					{
						return seat;
					}
				}
			}
			return null;
		}

		private function initSeats():void
		{
			currentClock = new Quad(10, 10, 0x54b2ff);
			addChild(currentClock);

			seats = new Vector.<UserSeat>();
			var mapWidth:int = CardUI.WIDTH * 9;
			var mapHeight:int = CardUI.HEIGHT * 5;
			for (var i:int = 0; i < 10; i++)
			{
				var seat:UserSeat = new UserSeat();
				if (i < 3) //下
				{
					seat.y = cardMap.y + mapHeight + 10;
					seat.x = cardMap.x + mapWidth * (i + 1) / 4 - UserSeat.WIDTH / 2;
				}
				else if (i < 5) //右
				{
					seat.y = cardMap.y + mapHeight * (i - 3 + 1) / 3 - UserSeat.HEIGHT / 2;
					seat.x = cardMap.x + mapWidth + 10;
				}
				else if (i < 8) //上
				{
					seat.y = cardMap.y - UserSeat.HEIGHT - 5;
					seat.x = cardMap.x + mapWidth * (i - 5 + 1) / 4 - UserSeat.WIDTH / 2;
				}
				else //左
				{
					seat.y = cardMap.y + mapHeight * (i - 8 + 1) / 3 - UserSeat.HEIGHT / 2;
					seat.x = cardMap.x - UserSeat.WIDTH - 5;
				}

				add(seat);
				seats.push(seat);
			}

			var orders:Vector.<UserOrderVo> = Me.Instace.orders;
			for each (var order:UserOrderVo in orders)
			{
				var user:User = ProxyHelper.getUser(order.id);
				if (user)
				{
					var currentSeat:UserSeat = seats[order.order];
					if (currentSeat)
					{
						currentSeat.attachUser(user);
					}
				}
			}

			setCurrentTurnUser(Me.Instace.currentTurn);
		}

		/**
		 * 设置当前轮到的人
		 * */
		public function setCurrentTurnUser(userId:int):void
		{
			var seat:UserSeat = getSeatById(userId);
			if (seat)
			{
				currentClock.x = seat.x + seat.width / 2 - currentClock.width / 2;
				currentClock.y = seat.y + 10;
			}
		}

		private function disposeGame():void
		{

		}

		public function initUI():void
		{
			startGameButton = new Button();
			startGameButton.label = "开始";
			startGameButton.isEnabled = false;
			addChild(startGameButton);

			exitRoomButton = new Button();
			exitRoomButton.label = "退出房间";
			addChild(exitRoomButton);

			messagePanel = new MessagePanel();
			addChild(messagePanel);

			userList = new ListEx();
			userList.setSize(600, 300);
			userList.itemRendererProperties.labelField = "name";
			userList.itemRendererType = UserPrepareItemRender;
			addChild(userList);

			var layout:TiledRowsLayout = new TiledRowsLayout();
			layout.gap = 10;
			layout.paddingTop = layout.paddingRight = layout.paddingBottom = layout.paddingLeft = 15;
			layout.useVirtualLayout = false;
			layout.typicalItemHeight = 130;
			layout.useSquareTiles = false;
			layout.typicalItemWidth = 135;
			layout.tileHorizontalAlign = TiledRowsLayout.TILE_HORIZONTAL_ALIGN_JUSTIFY;
			layout.tileVerticalAlign = TiledRowsLayout.TILE_VERTICAL_ALIGN_JUSTIFY;

			userList.layout = layout;

			resize();
			checkStartButton();
		}

		public function resize():void
		{
			startGameButton.setSize(90, 40);
			exitRoomButton.setSize(90, 40);
			messagePanel.setSize(300, 210);
			userList.setSize(400, 300);

			messagePanel.x = 30;
			messagePanel.y = SP.height - messagePanel.height - 15;

			exitRoomButton.x = SP.width - exitRoomButton.width - 40;
			exitRoomButton.y = SP.height - exitRoomButton.height - 20;

			startGameButton.x = exitRoomButton.x;
			startGameButton.y = exitRoomButton.y - startGameButton.height - 20;

		}
	}
}
